package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.mapper.ItemTrialMapper;
import com.ddwl.goods.model.ItemTrial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class ItemTrialDao extends TkDao<ItemTrialMapper, ItemTrial> {

    @Autowired
    private ItemTrialMapper itemTrialMapper;



    @Override
    public int insertList(List<ItemTrial> itemTrials){
        List<ItemTrial> itemTrialList=itemTrials.stream().map(itemTrial -> {
            itemTrial.setId(UUIDGenerator.getShortUUID());
            itemTrial.setGmtModified(Instant.now());
            itemTrial.setGmtCreated(Instant.now());
            itemTrial.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
            return itemTrial;
        }).collect(Collectors.toList());
        return itemTrialMapper.insertByList(itemTrialList);
    }
    /**
     * 下架审批驳回更新下架原因为null
     */
    public void updateUndercarriageReason(String itemId,Integer stage){
        itemTrialMapper.updateUndercarriageReason(itemId,stage);
    }

    /**
     * 申请下架原因更新为null
     */
    public void updateUndercarriageRefuseReasonNull(String itemId,Integer stage){
        itemTrialMapper.updateUndercarriageRefuseReasonNull(itemId,stage);
    }

    /**
     * 驳回原因更新为null
     */
    public void updateRefuseReasonNull(String itemId,Integer stage){
        itemTrialMapper.updateRefuseReasonNull(itemId,stage);
    }

    /**
     * 根据商品id和阶段更新
     */
    public  void updateListByItemIdAndStage(List<ItemTrial> itemTrials){
        List<ItemTrial> itemTrialList=itemTrials.stream().map(itemTrial -> {
            itemTrial.setGmtModified(Instant.now());
            return itemTrial;
        }).collect(Collectors.toList());
        itemTrialMapper.updateListByItemIdAndStage(itemTrialList);
    }

    /**
     * 更新创建初审审核通过
     */
    public void updatePass(List<String> itemIds){
        itemTrialMapper.updatePass(itemIds);
    }

    public List<ItemTrial> selectByItemIds(Collection<String> itemIds){
        Example example = getExample();
        example.and().andIn("itemId", itemIds);
        example.orderBy("gmtCreated").desc();
        return itemTrialMapper.selectByExample(example);
    }

    /**
     * 按商品id和审核阶段删除审核记录
     * @param
     */
    public void deleteAuditing(List<String> itemIds,Integer auditingStage){
        itemTrialMapper.deleteAuditing(itemIds,auditingStage);
    }

    /**
     * 批量更新商品上架状态
     * @param
     */
    public void  updateStackingStatus(List<String> itemIds,Integer isStacking){
        itemTrialMapper.updateStackingStatus(itemIds,isStacking);
    }

    public List<ItemTrial> getAll() {
        return itemTrialMapper.selectAll();
    }

    public void changeId(Map<String, String> map) {
        itemTrialMapper.changeId(map);
    }
}
